<?php

namespace app\common;

/**
 * Class Http
 */
class Http
{
    private static $info = [];

    /**
     * @param  $url
     * @param  array $headers
     * @param  array $options
     * @return \Requests_Response
     */
    public static function get($url, array $headers = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $options]);
    }

    /**
     * @param  $url
     * @param  array $headers
     * @param  array $options
     * @return \Requests_Response
     */
    public static function head($url, array $headers = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $options]);
    }

    /**
     * @param  $url
     * @param  array $headers
     * @param  array $options
     * @return \Requests_Response
     */
    public static function delete($url, array $headers = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $options]);
    }

    /**
     * @param  $url
     * @param  array $headers
     * @param  array $options
     * @return \Requests_Response
     */
    public static function trace($url, array $headers = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $options]);
    }

    /**
     * @param  $url
     * @param  array $headers
     * @param  array $data
     * @param  array $options
     * @return \Requests_Response
     */
    public static function post($url, array $headers = [], $data = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
    }

    /**
     * @param  $url
     * @param  array $headers
     * @param  array $data
     * @param  array $options
     * @return \Requests_Response
     */
    public static function put($url, array $headers = [], array $data = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
    }

    /**
     * @param  $url
     * @param  array $headers
     * @param  array $data
     * @param  array $options
     * @return \Requests_Response
     */
    public static function options($url, array $headers = [], array $data = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
    }

    /**
     * @param  $url
     * @param  $headers
     * @param  array $data
     * @param  array $options
     * @return \Requests_Response
     */
    public static function patch($url, array $headers, array $data = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
    }

    /**
     * @param  string $url
     * @param  array  $headers
     * @param  array  $data
     * @param  string $type
     * @param  array  $options
     * @return \Requests_Response
     */
    public static function request($url, array $headers = [], array $data = [], $type = \Requests::GET, array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $type, $options]);
    }

    private static function getHooks()
    {
        $hooks = new \Requests_Hooks();
        $hooks->register(
            'curl.after_request',
            function ($headers, $info) {
                self::$info = !is_array($info) ? [$info] : $info;
            }
        );

        return $hooks;
    }

    private static function setOptions($options)
    {
        if (!isset($options['hooks'])) {
            $options['hooks'] = self::getHooks();
        }
        $options['idn'] = false;
        return $options;
    }

    private static function call($name, $args)
    {
        try {
            $type = $name;

            if ('request' === $name) {
                $type = isset($args[3]) ? $args[3] : $name;
            }

            $type = strtolower($type);

            //设置url
            if ($type !== 'request_multiple') {
                $args[0] = new \Requests_IRI($args[0]);
            }
            $response = call_user_func_array(['Requests', $name], $args);
            if ($response instanceof \Requests_Response && is_object($response->url)) {
                $response->url = $response->url->uri;
            }

            return $response;
        } catch (\Exception $e) {
            \Log::error($e->getMessage());
            $response = new \Requests_Response();
            $response->body = '';

            return $response;
        }
    }
}
